package com.gzq.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gzq.entity.CommonCondition;
import com.gzq.entity.Doctor;
import com.gzq.entity.Hospital;
import com.gzq.service.DoctorService;
import com.gzq.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gzq.service.HospitalService;

@Controller
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private PageUtils pageUtils;

    // 医生主界面(推荐医生)
    @RequestMapping(value = "/doctorIndex/{page}")
    public String officeIdex(Model model, @PathVariable("page") int page) {     // @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中
        // 查询推荐的医院
        List<Hospital> hospitalRe = hospitalService.findHosByRe();
        // 设置页面
        pageUtils.setCurrentPage(page);
        pageUtils.setTotalRecord(doctorService.findDoctorByReNum(hospitalRe));

        // 防止第一页等情况的发生
        int start;
        if (pageUtils.getCurrentPage() == 0) {
            start = 0;
        } else {
            start = pageUtils.getPageRecord() * (pageUtils.getCurrentPage() - 1);
        }

        Map<String, Object> doctorMap = new HashMap<String, Object>();
        // .put方式也是添加，只不过put方法如果添加重复了，后面添加的值会覆盖掉原来的值(该方法返回原来的值)
        doctorMap.put("list", hospitalRe);
        doctorMap.put("start", start);
        doctorMap.put("size", pageUtils.getPageRecord());

        // 查询开通预约挂号的医生
        List<Doctor> doctorRe = doctorService.findDoctorByRe(doctorMap);
        model.addAttribute("pages", pageUtils);
        model.addAttribute("doctorRe", doctorRe);
        return "doctor/doctorIndex";
    }

    // 医生的详细信息
    @RequestMapping(value = "/doctorInfoShow/{id}", method = RequestMethod.GET)
    public String hosInfoShow(Model model, @PathVariable(value = "id") int id) {
        Doctor doctor = doctorService.findDoctorById(id);
        Hospital hospital = hospitalService.findHosByName(doctor.getHospitalName());
        model.addAttribute("hos", hospital);
        model.addAttribute("doctor", doctor);
        return "doctor/doctorInfoShow";
    }

    // 全部的医生
    @RequestMapping(value = "/allDoctor/{page}")
    public String orderOffcie(Model model, @PathVariable("page") int page, Doctor doctor) {
        // 通过输入条件查询到的结果传回前台
        CommonCondition commonCondition = new CommonCondition();
        commonCondition.setHospitalName(doctor.getHospitalName());
        commonCondition.setOfficesName(doctor.getOfficesName());
        commonCondition.setDoctorName(doctor.getDoctorName());
        commonCondition.setDoctorTitle(doctor.getDoctorTitle());
        commonCondition.setDoctorDegree(doctor.getDoctorDegree());
        commonCondition.setDoctorAdministrative(doctor.getDoctorAdministrative());

        // 设置当前页码
        pageUtils.setCurrentPage(page);
        pageUtils.setTotalRecord(doctorService.findDoctorNum(doctor));

        int start;
        if (pageUtils.getCurrentPage() == 0) {
            start = 0;
        } else {
            start = pageUtils.getPageRecord() * (pageUtils.getCurrentPage() - 1);
        }

        List<Doctor> doctorRe = doctorService.findDoctorByCondition(doctor, start, pageUtils.getPageRecord());
        // 查询医生的职位
        List<String> doctorTitle = doctorService.findDoctorTitle();
        List<String> doctorAdministrative = doctorService.findDoctorAdministrative();
        List<String> doctorDegree = doctorService.findDoctorDegree();
        model.addAttribute("pages", pageUtils);
        model.addAttribute("doctorRe", doctorRe);

        // 查询条件
        // model是前后台传输的背包(起到装前后台数据的效果)
        model.addAttribute("commonCondition", commonCondition);
        // 将查询的医生职称传到前台
        model.addAttribute("doctorTitle", doctorTitle);
        model.addAttribute("doctorAdministrative", doctorAdministrative);
        model.addAttribute("doctorDegree", doctorDegree);
        return "doctor/doctor";
    }

}
